<?php
/***************************************************************************
 *                               pirep.php
 *                            -------------------
 *                       (APU) Automated Pilot Utility
 *   begin                : Sunday Nov 26, 2006
 *   copyright            : (C) 2004-2006 SF Games
 *   email                : techsupport@sf-games.com
 ****************************************************************************/
 
 	include('include/common.php');
 	include('include/config.php');
	include('include/dbcommon.php');

	$bEditMode = false;

	// An array to hold a PIREP info
	$pirep = array("PILOT_ID" => "", "FLIGHT_DATE" => "", "AC_TYPE" => "", "TAIL_NUM" => "", "FLIGHT_NUM" => "", "DEPART" => "", "DEST" => "", "TIME_OUT" => "", "TIME_IN" => "", "FUEL_OUT" => "", "FUEL_IN" => "", "PAX" => "", "CARGO" => "", "COMMENTS" => "");

	// Get any PIREP ID that would come from the pilotinfo page
	$pirepID = stripslashes($_GET['pirepid']);

	// If passed a PIREP ID, we will be in edit mode
	if ( strlen($pirepID) > 0 )
	{
		$bEditMode = true;

		$db = mysql_connect($dbhost, $dbuname, $dbpass)
			or die("Error: in pirep_submit.php. Could not connect to the SQL server " .mysql_error());
		mysql_select_db($dbname);

		$query  = 'SELECT PILOT_ID, FLIGHT_DATE, AC_TYPE, TAIL_NUM, FLIGHT_NUM, DEPART, DEST, TIME_OUT, TIME_IN, FUEL_OUT, FUEL_IN, PAX, CARGO, COMMENTS FROM ' .$table_prefix .'PIREP WHERE id = ' .sqlfix($pirepID);
		$result = mysql_query($query);
		if ( !$result )
			die("Error: in sendPilotEmail() from pilotemail_submit.php. Query for Pilot ID in table PILOTS failed. " .mysql_error());
		$pirep = mysql_fetch_array($result, MYSQL_ASSOC);
	}

	htmlHeader();
	vaTable($vaName);
	displayPIREP($pirep, $pirepID, $bEditMode);
 	htmlFooter();
?>


<?php
 
 			//////////////////////////////////////////////////
 			//                                              //
 			//             Support Functions                //
 			//                                              //
 			//////////////////////////////////////////////////
 
 //
 // Display rosters HTML header
 //
 function htmlHeader()
 {
 	echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
 	echo "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n";
 	echo "<head>\n";
 	echo "<title>APU PIREP (Pilot Report Submission Form)</title>\n";
 	include_once('include/header.php');
 	echo "</head>\n";
 	echo "<body>\n";
 	echo "<div align=\"center\">\n";
 }
 
 //
 // Display the rosters HTML footer
 //
 function htmlFooter()
 {
 	global $rosterNavigateTo;
 	global $rosterNavigateVerbiage;
 	global $APU_COPYRIGHT;
 
 	// Display return to another page link/table
 	echo "	<table class=\"table_roster_footer\">\n";
 	echo "	<tbody>\n";
 	echo "		<tr>\n";
 	echo "			<td><div align=\"center\"><span class=\"textdark\">Click <a href=\"" .$rosterNavigateTo ."\">here</a> " .$rosterNavigateVerbiage ."</span></div></td>\n";
 	echo "		</tr>\n";
 	echo "	</tbody>\n";
 	echo "	<tfoot><tr><td colspan=\"1\"></td></tr></tfoot>\n";
 	echo "	</table>\n";
 
 	// Display Copyright
 	echo "<p>" .$APU_COPYRIGHT ."</p>\n";
 
 	// End HTML
 	echo "</div>\n";
 	echo "</body>\n";
 	echo "</html>\n";
 }
 
 //
 // Display VA name in top table
 //
 function vaTable($vaName)
 {
 	echo "	<p>&nbsp;</p>\n";
 	echo "	<table class=\"table_roster_header\">\n";
 	echo "		<tbody>\n";
 	echo "			<tr>\n";
 	echo "				<td><div align=\"center\"><span class=\"textredBLrg\">" .$vaName ."<br/>PIREP Submission</span></div></td>\n";
 	echo "			</tr>\n";
 	echo "		</tbody>\n";
 	echo "		<tfoot><tr><td></td></tr></tfoot>\n";
 	echo "	</table>\n";
 	echo "	<p>&nbsp;</p>\n";
}

//
// Displays the PIREP Submission form
//
// Form will have only todays date if called directly from main menu
// Or will be filled with a previous PIREP's values, if editing a PIREP
//
function displayPirep($pirep, $pirepID, $bEditMode)
{
	include('include/config.php');

	echo "<table class=\"table_loa\">\n";
    echo "<thead><tr><th>&nbsp;</th></tr></thead>\n";
    echo "<tbody>\n";
    echo "  <tr>\n";
    echo "    <td class=\"tableTextCenter\">\n";
	echo "	<form name=\"pirep_form\" id=\"pirep_form\" method=\"post\" action=\"include/pirep_submit.php\">\n";
	echo "		  <table class=\"table_pirep\">\n";
	echo "			  <tbody>\n";
	echo "					<tr>\n";
	echo "						<td class=\"textdarkRight\">Pilot ID</td>\n";
	echo "						<td><div align=\"center\"><input name=\"pilot_id\" type=\"text\" class=\"editfield\" id=\"pilot_id\" size=\"20\" maxlength=\"15\" value=\"" .$pirep['PILOT_ID'] ."\" /></div></td>\n";
	echo "						<td class=\"textdark\">CA2535DEN</td>\n";
	echo "					</tr>\n";
	echo "					<tr>\n";
	echo "						<td class=\"textdarkRight\">Password</td>\n";
	echo "						<td><div align=\"center\"><input name=\"pilot_pass\" type=\"password\" class=\"editfield\" id=\"pilot_pass\" size=\"20\" maxlength=\"40\" /></div></td>\n";
	echo "						<td class=\"textdark\">Your Password</td>\n";
	echo "					</tr>\n";
	echo "					<tr>\n";
	echo "						<td class=\"textdarkRight\">Date</td>\n";
	echo "						<td><div align=\"center\">\n";

	if ( $bEditMode )
	{
		echo "<input name=\"flight_date\" type=\"text\" class=\"editfield\" id=\"flight_date\" size=\"20\" maxlength=\"10\" value=\"" .$pirep['FLIGHT_DATE'] ."\" />\n";
	}
	else
	{
		// Display todays date in the format set in config.php
		$today  = getdate();
		if ( $bUseEuroDateFormat ) $sToday = $today["mday"] ."/" .$today["mon"]  ."/" .$today["year"];
		else   					   $sToday = $today["mon"]  ."/" .$today["mday"] ."/" .$today["year"];
		echo "<input name=\"flight_date\" type=\"text\" class=\"editfield\" id=\"flight_date\" size=\"20\" maxlength=\"10\" value=\"" .$sToday ."\" />\n";
		echo "						</div></td>\n";
	}

	if ( $bUseEuroDateFormat )
		echo "<td class=\"textdark\"> 24/5/2006 </td>\n";
	else
		echo "<td class=\"textdark\"> 05/24/2006 </td>\n";

	echo "					</tr>\n";
	echo "					<tr>\n";
	echo "						<td class=\"textdarkRight\">Aircraft Type </td>\n";
	echo "						<td><div align=\"center\"><input name=\"ac_type\" type=\"text\" class=\"editfield\" id=\"ac_type\" size=\"20\" maxlength=\"20\" value=\"" .$pirep['AC_TYPE'] ."\" /></div></td>\n";
	echo "						<td class=\"textdark\"> B747-400F</td>\n";
	echo "					</tr>\n";
	echo "					<tr>\n";
	echo "						<td class=\"textdarkRight\">Tail # </td>\n";
	echo "						<td><div align=\"center\"><input name=\"tail_num\" type=\"text\" class=\"editfield\" id=\"tail_num\" size=\"20\" maxlength=\"20\" value=\"" .$pirep['TAIL_NUM'] ."\" /></div></td>\n";
	echo "						<td class=\"textdark\">N1234CA</td>\n";
	echo "					</tr>\n";
	echo "					<tr>\n";
	echo "						<td class=\"textdarkRight\">Flight #</td>\n";
	echo "						<td><div align=\"center\"><input name=\"flight_num\" type=\"text\" class=\"editfield\" id=\"flight_num\" size=\"20\" maxlength=\"20\" value=\"" .$pirep['FLIGHT_NUM'] ."\" /></div></td>\n";
	echo "						<td class=\"textdark\">1027</td>\n";
	echo "					</tr>\n";
	echo "					<tr>\n";
	echo "						<td class=\"textdarkRight\">Depart ICAO</td>\n";
	echo "						<td><div align=\"center\"><input name=\"departure\" type=\"text\" class=\"editfield\" id=\"departure\" size=\"20\" maxlength=\"4\" value=\"" .$pirep['DEPART'] ."\" /></div></td>\n";
	echo "						<td class=\"textdark\">KDEN</td>\n";
	echo "					</tr>\n";
	echo "					<tr>\n";
	echo "						<td class=\"textdarkRight\">Dest ICAO </td>\n";
	echo "						<td><div align=\"center\"><input name=\"destination\" type=\"text\" class=\"editfield\" id=\"destination\" size=\"20\" maxlength=\"4\" value=\"" .$pirep['DEST'] ."\" /></div></td>\n";
	echo "						<td class=\"textdark\">EGLL</td>\n";
	echo "					</tr>\n";
	echo "					<tr>\n";
	echo "						<td class=\"textdarkRight\">Time Out </td>\n";
	echo "						<td><div align=\"center\"><input name=\"time_out\" type=\"text\" class=\"editfield\" id=\"time_out\" size=\"20\" maxlength=\"4\" value=\"" .$pirep['TIME_OUT'] ."\" /></div></td>\n";
	echo "						<td class=\"textdark\">1400 (GMT)</td>\n";
	echo "					</tr>\n";
	echo "					<tr>\n";
	echo "						<td class=\"textdarkRight\">Time In </td>\n";
	echo "						<td><div align=\"center\"><input name=\"time_in\" type=\"text\" class=\"editfield\" id=\"time_in\" size=\"20\" maxlength=\"4\" value=\"" .$pirep['TIME_IN'] ."\" /></div></td>\n";
	echo "						<td class=\"textdark\">2025 (GMT)</td>\n";
	echo "					</tr>\n";
	echo "					<tr>\n";
	echo "						<td class=\"textdarkRight\">Fuel Out </td>\n";
	echo "						<td><div align=\"center\"><input name=\"fuel_out\" type=\"text\" class=\"editfield\" id=\"fuel_out\" size=\"20\" maxlength=\"7\" value=\"" .$pirep['FUEL_OUT'] ."\" /></div></td>\n";
	echo "						<td class=\"textdark\">250000 (lbs)</td>\n";
	echo "					</tr>\n";
	echo "					<tr>\n";
	echo "						<td class=\"textdarkRight\">Fuel In </td>\n";
	echo "						<td><div align=\"center\"><input name=\"fuel_in\" type=\"text\" class=\"editfield\" id=\"fuel_in\" size=\"20\" maxlength=\"7\" value=\"" .$pirep['FUEL_IN'] ."\" /></div></td>\n";
	echo "						<td class=\"textdark\">8943 (lbs) </td>\n";
	echo "					</tr>\n";
	echo "					<tr>\n";
	echo "						<td class=\"textdarkRight\">PAX</td>\n";
	echo "						<td><div align=\"center\"><input name=\"pax\" type=\"text\" class=\"editfield\" id=\"pax\" size=\"20\" maxlength=\"4\" value=\"" .$pirep['PAX'] ."\" /></div></td>\n";
	echo "						<td class=\"textdark\">125 (Passengers) </td>\n";
	echo "					</tr>\n";
	echo "					<tr>\n";
	echo "						<td class=\"textdarkRight\">Cargo</td>\n";
	echo "						<td><div align=\"center\"><input name=\"cargo\" type=\"text\" class=\"editfield\" id=\"cargo\" size=\"20\" maxlength=\"7\" value=\"" .$pirep['CARGO'] ."\" /></div></td>\n";
	echo "						<td class=\"textdark\">5000 (lbs) </td>\n";
	echo "					</tr>\n";
	echo "					<tr>\n";
	echo "						<td class=\"textdarkRight\">Comments</td>\n";
	echo "						<td><div align=\"center\"><textarea name=\"comments\" cols=\"25\" rows=\"4\" class=\"editfield\" id=\"comments\">" .$pirep['COMMENTS'] ."</textarea></div></td>\n";
	echo "						<td class=\"textdark\">Weather, ATC </td>\n";
	echo "					</tr>\n";
	echo "			</tbody>\n";
	echo "				<tfoot>\n";
	echo "					<tr>\n";
	echo "						<td colspan=\"3\"><div align=\"center\">\n";

	// Pass what format the date is in
	if ( $bUseEuroDateFormat )
		echo "<input name=\"date_format\" type=\"hidden\" value=\"european\" />\n";
	else
		echo "<input name=\"date_format\" type=\"hidden\" value=\"us\" />\n";

	echo "<input name=\"pirepID\" type=\"hidden\" value=\"" .$pirepID ."\" />\n";
	echo "<input name=\"editmode\" type=\"hidden\" value=\"" .$bEditMode ."\" />\n";
	echo "<input name=\"referrer\" type=\"hidden\" value=\"" .$_SERVER['REQUEST_URI'] ."\" />\n";
	echo "							<input name=\"Submit\" type=\"image\" id=\"Submit\" src=\"img/submit_button.jpg\" alt=\"Submit\" />\n";
	echo "						</div></td>\n";
	echo "					</tr>\n";
	echo "				</tfoot>\n";
	echo "		</table>\n";
    echo "   	  </form>\n";
	echo "	</td>\n";
    echo "  </tr>\n";
    echo "</tbody>\n";
    echo "<tfoot>\n";
    echo "  <tr>\n";
    echo "    <td colspan=\"1\">\n";
	echo "		<div align=\"center\"><a href=\"changepass.php\">Change your password</a> here.<br />Click <a href=\"index.php\">here</a> to return to the Pilots Room.</div>\n";
	echo "	</td>\n";
    echo "  </tr>\n";
    echo "</tfoot>\n";
    echo "</table>\n";

	echo "<script type=\"text/javascript\">\n";
	echo "<!--\n";
	echo "// Position the cursor on the first form control\n";
	echo "   document.forms[0].elements[0].focus()\n";
	echo "-->\n";
	echo "</script>\n";
}
